package com.hngc.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hngc.entity.Road;
import com.hngc.service.IRoadService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-10-20
 */
@RestController
@RequestMapping("/road")
public class RoadController {
    @Autowired
    IRoadService roadService;

    @RequestMapping(value = "/list")
    public PageInfo<Road> list(Integer pageNum, Integer pageSize) {
        List<Road> roads = null;
        if (pageNum == null) {
            pageNum = 0;
        }
        if (pageSize != null && pageSize != 0)
            PageHelper.startPage(pageNum, pageSize);
        try {
            roads = roadService.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageInfo<Road> list = new PageInfo<>(roads);
        return list;
    }

    @RequestMapping(value = "/save")
    public String save(Road road) {
        int i = 0;
        JSONObject jsonObject = new JSONObject();
        try {
            roadService.save(road);
            i = 1;

        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("message", e);
        }
        jsonObject.put("state", i);
        return jsonObject.toString();
    }

    @RequestMapping(value = "/getone")
    public Road getOne(String id) {
        Road road = null;

        try {
            road = roadService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return road;
    }

    @RequestMapping(value = "/update")
    public String update(Road road) {
        int i = 0;
        JSONObject jsonObject = new JSONObject();
        try {
            roadService.updateById(road);
            i = 1;

        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("message", e);
        }
        jsonObject.put("state", i);
        return jsonObject.toString();
    }

    @RequestMapping(value = "/delete")
    public String del(String id) {
        int i = 0;
        JSONObject jsonObject = new JSONObject();
        try {
            roadService.removeById(id);
            i = 1;

        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("message", e);
        }
        jsonObject.put("state", i);
        return jsonObject.toString();
    }

}
